package cn.lili.listener;

import cn.lili.cache.impl.RedisCache;
import cn.lili.modules.order.order.client.RankingDistributionClient;
import cn.lili.modules.order.order.entity.dto.RankingFySetParentChildCountDTO;
import cn.lili.routing.UserRoutingKey;
import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
@Slf4j
@RequiredArgsConstructor
public class RankingDistributionListener {

    /**
     * 店主排名活动上级绑定下级人数
     */
    private final RankingDistributionClient rankingDistributionClient;
    private final RedisCache redisCache;


    /**
     * 飞语绑定上级,检测是否是商城店主,并对商城的店主执行下级+1操作
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "${lili.amqp.ranking}" + "_" + UserRoutingKey.BIND_PARENT),
            exchange = @Exchange(value = "${lili.amqp.ranking}"),
            key = UserRoutingKey.BIND_PARENT))
    public void fySetParentSyncMallRankingDistribution(@Payload JSONObject jsonObject) {
        try {
            log.info("RankingDistributionListener: #fySetParentSyncMallRankingDistribution# 参数为,{}", null != jsonObject ? jsonObject.toJSONString() : "null");
            if (null == jsonObject) {
                return;
            }
            log.info("RankingDistributionListener: #fySetParentSyncMallRankingDistribution# 转换完成后JSONObject结果为:{}", jsonObject.toJSONString());
            RankingFySetParentChildCountDTO dto = new RankingFySetParentChildCountDTO();
            dto.setNum(jsonObject.getInteger("num"));
            dto.setParentOpenId(jsonObject.getString("parentOpenId"));
            dto.setChildOpenId(jsonObject.getString("childOpenId"));
            String updateSyncMallRankingDistributionKey = "updateSyncMallRankingDistributionKey:" + dto.getParentOpenId() + "_" + dto.getChildOpenId();
            if (!redisCache.exists(updateSyncMallRankingDistributionKey)) {
                boolean flag = rankingDistributionClient.fySetParentSyncMallRankingDistribution(dto);
                if (flag) {
                    redisCache.put(updateSyncMallRankingDistributionKey, 1, 20L, TimeUnit.MINUTES);
                }
                log.info("RankingDistributionListener: #fySetParentSyncMallRankingDistribution# 更新下级人数已成功消费,{},条件为,{}", (flag ? "成功" : "失败"), jsonObject.toJSONString());
            }else{
                log.info("RankingDistributionListener: #fySetParentSyncMallRankingDistribution# 更新下级人数已成功消费,不重复累加");
            }
        } catch (Exception e) {
            log.error("飞语绑定上级,检测是否是商城店主,并对商城的店主执行下级+1操作,事件执行异常:{}", e.getMessage());
        }

    }
}
